package com.viego.financialrecommendsystem.controller;

import com.viego.financialrecommendsystem.common.AjaxResult;
import com.viego.financialrecommendsystem.entity.Comment;
import com.viego.financialrecommendsystem.entity.Discuss;
import com.viego.financialrecommendsystem.service.IDiscussService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/discuss")
public class DiscussController{

    @Resource
    IDiscussService discussService;

    @GetMapping("/query")
    public AjaxResult query(@RequestParam("discussId") String  discussId){
        long discussID = Long.parseLong(discussId);
        Discuss discuss = discussService.queryById(discussID);
        return AjaxResult.success("查询成功",discuss);
    }

    @GetMapping("/queryComment")
    public AjaxResult queryComment(@RequestParam("discussId") String discussId){
        List<Comment> list =  discussService.comment(Long.parseLong(discussId));
        return AjaxResult.success("查询成功",list);
    }

    @GetMapping("/collect")
    public AjaxResult collect(@RequestParam("discussId") String discussId){
        Integer number = discussService.collect(Long.parseLong(discussId));
        return AjaxResult.success("查询成功",number);
    }

    @PostMapping("/submitComment")
    public AjaxResult submitComment(@RequestBody Comment comment){
        log.info("正在尝试提交评论");
        log.info("comment = {}",comment);
       boolean flag =  discussService.saveComment(comment);
       if (flag){
           return AjaxResult.success("提交成功");
       }else{
           return AjaxResult.error("提交失败");
       }
    }


    @GetMapping("addCollect")
    public AjaxResult addCollect(@RequestParam("userId") String userId,
                                 @RequestParam("discussId") String discussId){
        //先检查一下是否已经被收藏了，
        boolean f = discussService.isCollected(Long.parseLong(userId),Long.parseLong(discussId));
        if (f)
            return AjaxResult.error("无需重复收藏");
        boolean flag = discussService.addCollect(userId,discussId);
        if (flag){
            return AjaxResult.success("收藏成功");
        }else{
            return AjaxResult.error("收藏失败");
        }
    }

    @GetMapping("/Mydiscuss")
    public AjaxResult MyDiscuss(@RequestParam("userId")String userId){
        List<Discuss> list = discussService.article(Long.parseLong(userId));
        return AjaxResult.success("查询成功",list);
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody Discuss discuss){
        boolean flag = discussService.save(discuss);
        if (flag){
            return AjaxResult.success("发表成功");
        }else{
            return AjaxResult.error("发表失败");
        }
    }

    @GetMapping("/type")
    public AjaxResult Type(){
        List<String> types = discussService.type();
        return AjaxResult.success("查询成功",types);
    }

    @GetMapping("/removeCollect")
    public AjaxResult removeCollect(@RequestParam("userId")String userId,
                                    @RequestParam("discussId")String discussId){
        boolean flag = discussService.removeCollect(Long.parseLong(userId),Long.parseLong(discussId));
        if (flag){
            return AjaxResult.success("操作成功");
        }else{
            return AjaxResult.error("操作失败");
        }
    }

}
